package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmCarLicensingAddr;
import com.car.loan.project.cm.service.ICmCarLicensingAddrService;
import com.car.loan.project.cm.util.bo.CmCarLicensingAddrAddBo;
import com.car.loan.project.cm.util.bo.CmCarLicensingAddrUpdateBo;
import com.car.loan.project.cm.util.select.CarLicensingAddrBaseSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/23 21:19
 */
@RestController
@RequestMapping("/cm/carLicensingAddr")
@Api(value = "车辆上牌地址信息管理", tags = "基础数据-车辆上牌地址信息管理接口")
public class CmCarLicensingAddrController extends BaseController {

    @Autowired
    private ICmCarLicensingAddrService carLicensingAddrService;

    @PreAuthorize("@ss.hasPermi('cm:carLicensingAddr:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取车辆上牌地址信息列表", responseContainer = "list", response = CmCarLicensingAddr.class)
    public AjaxResult list(@Valid CarLicensingAddrBaseSelect carLicensingAddrBaseSelect) {
        IPage<CmCarLicensingAddr> page = new Page<>(carLicensingAddrBaseSelect.getPageNum(),
            carLicensingAddrBaseSelect.pageSize);
        CmCarLicensingAddr cmCarLicensingAddr = new CmCarLicensingAddr();
        cmCarLicensingAddr.setParentId(carLicensingAddrBaseSelect.getParentId());
        if (null != carLicensingAddrBaseSelect.getStatus()) {
            cmCarLicensingAddr.setStatus(carLicensingAddrBaseSelect.getStatus());
        }
        QueryWrapper<CmCarLicensingAddr> queryWrapper = new QueryWrapper<>(cmCarLicensingAddr);
        if (StringUtils.isNotEmpty(carLicensingAddrBaseSelect.getName())) {
            queryWrapper.like("name", carLicensingAddrBaseSelect.getName());
        }
        return AjaxResult.success(carLicensingAddrService.selectPage(page, queryWrapper));
    }


    @PreAuthorize("@ss.hasPermi('cm:carLicensingAddr:query')")
    @GetMapping(value = "/{addrId}")
    @ApiOperation(value = "获取车辆上牌地址信息详情", response = CmCarLicensingAddr.class)
    public AjaxResult getInfo(@PathVariable Long addrId) {
        return AjaxResult.success(carLicensingAddrService
            .selectCmCarLicensingAddrById(addrId));
    }


    @PreAuthorize("@ss.hasPermi('cm:carLicensingAddr:add')")
    @Log(title = "车辆上牌地址信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加车辆上牌地址信息")
    public AjaxResult add(@RequestBody @Valid CmCarLicensingAddrAddBo cmCarLicensingAddrAddBo) {
        if (null != cmCarLicensingAddrAddBo.getParentId()
            && cmCarLicensingAddrAddBo.getParentId() != 0) {
            CmCarLicensingAddr cmCarLicensingAddr = carLicensingAddrService
                .selectCmCarLicensingAddrById(cmCarLicensingAddrAddBo.getParentId());
            if (null == cmCarLicensingAddr) {
                return AjaxResult.error("上级不存在");
            }
            if (cmCarLicensingAddr.getType() == 1) {
                return AjaxResult.error("该类型不可添加下级");
            }
            if (cmCarLicensingAddr.getParentId() != 0) {
                return AjaxResult.error("暂时不支持2级以上分类");
            }
        } else {
            cmCarLicensingAddrAddBo.setParentId(0L);
        }
        CmCarLicensingAddr cmCarLicensingAddr = new CmCarLicensingAddr();
        cmCarLicensingAddr.setParentId(cmCarLicensingAddrAddBo.getParentId());
        BeanUtils.copyBeanProp(cmCarLicensingAddr, cmCarLicensingAddrAddBo);
        return toAjax(carLicensingAddrService.insertCmCarLicensingAddr(cmCarLicensingAddr));
    }

    @PreAuthorize("@ss.hasPermi('cm:carLicensingAddr:edit')")
    @Log(title = "车辆上牌地址信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改车辆上牌地址信息")
    public AjaxResult edit(
        @RequestBody @Valid CmCarLicensingAddrUpdateBo cmCarLicensingAddrUpdateBo) {

        CmCarLicensingAddr cmCarLicensingAddr = carLicensingAddrService
            .selectCmCarLicensingAddrById(cmCarLicensingAddrUpdateBo.getId());
        if (null == cmCarLicensingAddr) {
            return AjaxResult.error("不存在该车辆上牌地址信息");

        }
        cmCarLicensingAddr.setStatus(cmCarLicensingAddrUpdateBo.getStatus());
        cmCarLicensingAddr.setAbbr(cmCarLicensingAddrUpdateBo.getAbbr());
        cmCarLicensingAddr.setName(cmCarLicensingAddrUpdateBo.getName());
        cmCarLicensingAddr.setType(cmCarLicensingAddrUpdateBo.getType());
        if (cmCarLicensingAddrUpdateBo.getType() == 1) {
            CmCarLicensingAddr cmCarLicensingAddrQuery = new CmCarLicensingAddr();
            cmCarLicensingAddrQuery.setParentId(cmCarLicensingAddrUpdateBo.getId());
            List<CmCarLicensingAddr> cmCarLicensingAddrs = carLicensingAddrService
                .selectCmCarLicensingAddrList(cmCarLicensingAddrQuery);
            if (null != cmCarLicensingAddrs && cmCarLicensingAddrs.size() > 0) {
                return AjaxResult.error("该信息下有子类，不可改变类型");
            }
        }
        return toAjax(carLicensingAddrService.updateCmCarLicensingAddr(cmCarLicensingAddr));
    }

//    @PreAuthorize("@ss.hasPermi('cm:carLicensingAddr:delete')")
//    @DeleteMapping(value = "/{addrId}")
//    @ApiOperation("删除对应车牌信息")
//    public AjaxResult delete(@PathVariable Long addrId) {
//        return toAjax(carLicensingAddrService.deleteCmCarLicensingAddrById(addrId));
//    }

}
